(function() {
	var $window = $(window);
	// 获取分类树
	function _get_classify_tree() {
		return new Promise(function(resolve, reject) {
			coAjax.get(appConfig.bus_classify_tree, {
				bus_id: busInfo._id
			}, function(classify_tree) {
				App.set("$Cache.classify.tree", classify_tree.result);
				resolve(classify_tree.result);
			});
		})
	};


	Path.on("/__basename__.html", function(_current_location) {
		var qs = _current_location.query;
		var root_id = qs.get("root_id");
		var second_id = qs.get("second_id");
		var classify_id = qs.get("classify_id") || second_id || root_id;
		// 获取二层分类
		function _get_second_classify() {
			return new Promise(function(resolve, reject) {
				_get_classify_tree().then(function(root_data) {
					root_id = root_id || root_data.childs[0].classify._id;
					coAjax.get(appConfig.bus_classify_by_id + root_id, {
						bus_id: busInfo._id
					}, function(root) {
						App.set("$Cache.classify.pointer_classify", root.result);
						resolve(root.result);
					});
				});
			});
		};
		_get_second_classify().then(function(pointer_data) {
			// 最后加载第三层分类 
			second_id = second_id || pointer_data.child_classify_list[0]._id
			coAjax.get(appConfig.bus_classify_by_id + second_id, {
				bus_id: busInfo._id
			}, function(classify_list) {
				App.set("$Cache.classify.pointer_second_classify", classify_list.result);
				if (!classify_id) {
					coAjax.get(appConfig.bus_classify_all_child_goods_by_id + classify_list.result.current_classify._id, {
						bus_id: busInfo._id
					}, function(goods) {
						App.set("$Cache.classify.title", classify_list.result.current_classify.classify_name);
						App.set("$Cache.classify.goods_list", goods.result);
					});
				};
			});
		});
		// 通过最底层获取商品信息
		if (classify_id) {
			App.set("$Cache.classify.goods_list", []);
			App.set("$Cache.classify.isloading", true);
			coAjax.get(appConfig.bus_classify_by_id + classify_id, {
				bus_id: busInfo._id
			}, function(classify) {
				App.set("$Cache.classify.title", classify.result.current_classify.classify_name);
				coAjax.get(appConfig.bus_classify_all_child_goods_by_id + classify_id, {
					bus_id: busInfo._id
				}, function(goods) {
					App.set("$Cache.classify.goods_list", goods.result);
					App.set("$Cache.classify.isloading", false);
				});
			});
		};
	});
}());